import Axios from 'axios'
const host = window.location.protocol + "//" + window.location.host;
const baseIp = process.env.NODE_ENV == 'development' ? '/tent' : host;
const service = Axios.create({
    baseURL: baseIp,
    timeout: 10 * 60 * 1000,
    withCredentials: true // 跨域请求时发送cookie
})
Axios.defaults.headers.post['Content-Type'] = 'application/json';

service.interceptors.request.use(
    config => {
        return config
    },
    error => {
        return Promise.reject(error.response)
    }
)
service.interceptors.response.use(
    response => {
        // console.log(response, '1111111')
        if (response.status === 200 || response.status == 201) {
            return response.data
        } else {
            throw new Error(response.status)
        }
    },
    error => {
        // console.log(error, '9090')
        return Promise.reject({ code: 500, msg: error })
    }
)

export default (url, method, submitData) => {
    return service({
        url,
        method,
        [method.toLowerCase() === 'post' ? 'data' : 'params']: submitData,
    });
};
